public class InvertBinaryTree {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        // 优化: 叶节点无需交换
        if(root.left == null && root.right == null) {
            return root;
        }

        // 交换左右子树
        TreeNode cur = root.left;
        root.left = root.right;
        root.right = cur;

        // 当前根节点的左右子树执行同样的逻辑
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

